﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace CPPEI.WebApi.Runtime
{
    class ModuleRegInfo
    {
        public string Name { get; set; }
        public Assembly Assembly { get; set; }

        public List<ScenarioRegInfo> Scenarios { get; private set; }

        public ModuleRegInfo()
        {
            Scenarios = new List<ScenarioRegInfo>();
        }

        /// <summary>
        /// 添加场景如果已存在就直接获取
        /// </summary>
        /// <param name="scenarioName"></param>
        /// <returns></returns>
        public ScenarioRegInfo AddOrGet(string scenarioName)
        {
            ScenarioRegInfo scenario = null;
            for (int index = 0; index < Scenarios.Count; index++)
            {
                if(string.Equals(Scenarios[index].Name, scenarioName, StringComparison.CurrentCultureIgnoreCase))
                {
                    scenario= Scenarios[index];
                    break;
                }
            }

            if(scenario == null)
            {
                scenario = new ScenarioRegInfo(scenarioName);
                Scenarios.Add(scenario);
            }
            return scenario;
        }

        public ActionRegInfo GetAction(string scenario, string action)
        {
            for (int index = 0; index < Scenarios.Count; index++)
            {
                if (string.Equals(Scenarios[index].Name, scenario, StringComparison.CurrentCultureIgnoreCase))
                {
                    ScenarioRegInfo bingoScenario = Scenarios[index];
                    for (int actionIndex = 0; actionIndex < bingoScenario.Actions.Count; actionIndex++)
                    {
                        if (string.Equals(bingoScenario.Actions[actionIndex].Name, action, StringComparison.CurrentCultureIgnoreCase))
                        {
                            return bingoScenario.Actions[actionIndex];
                        }
                    }
                }
            }

            return null;
        }
    }
}
